#include "server.h"
#include <QDebug>
#include <QCoreApplication>
//////////////////////////////////////////////////////
/// 工具函数. 后续很多模块可能都要用到
//////////////////////////////////////////////////////

static inline QString getFileName(const QString& path) {
    QFileInfo fileInfo(path);
    return fileInfo.fileName();
}

// 封装一个 "宏" 作为打印日志的方式.
#define TAG QString("[%1:%2]").arg(getFileName(__FILE__), QString::number(__LINE__))
// #define TAG "[" << __LINE__ << "]"

// qDebug 打印字符串的时候, 就会自动加上 " "
#define LOG() qDebug().noquote() << TAG

// 要求函数的定义如果写在 .h 中, 必须加 static 或者 inline (当然两个都加也可以), 避免链接阶段出现 "函数重定义" 的问题.
static inline QString formatTime(int64_t timestamp) {
    // 先把时间戳, 转换成 QDateTime 对象
    QDateTime dateTime = QDateTime::fromSecsSinceEpoch(timestamp);
    // 把 QDateTime 对象转成 "格式化时间"
    return dateTime.toString("MM-dd HH:mm:ss");
}

// 通过这个函数得到 秒级 的时间
static inline int64_t getTime() {
    return QDateTime::currentSecsSinceEpoch();
}

// 根据 QByteArray, 转成 QIcon
static inline QIcon makeIcon(const QByteArray& byteArray) {
    QPixmap pixmap;
    pixmap.loadFromData(byteArray);
    QIcon icon(pixmap);
    return icon;
}

// 读写文件操作.
// 从指定文件中, 读取所有的二进制内容. 得到一个 QByteArray
static inline QByteArray loadFileToByteArray(const QString& path) {
    QFile file(path);
    bool ok = file.open(QFile::ReadOnly);
    if (!ok) {
        LOG() << "文件打开失败!";
        return QByteArray();
    }
    QByteArray content = file.readAll();
    file.close();
    return content;
}

// 把 QByteArray 中的内容, 写入到某个指定文件里
static inline void writeByteArrayToFile(const QString& path, const QByteArray& content) {
    QFile file(path);
    bool ok = file.open(QFile::WriteOnly);
    if (!ok) {
        LOG() << "文件打开失败!";
        return;
    }
    file.write(content);
    file.flush();
    file.close();
}

//////////////////////////////////////////////////////////////////////////
///一些辅助函数
/// //////////////////////////////////////////////////////////////////////

bite_im::UserInfo makeUserInfo(int index,const QByteArray& avatar)
{
    bite_im::UserInfo userInfo;
    userInfo.setUserId(QString::number( 666 + index));
    userInfo.setNickname("一罪与百善" + QString::number(index));
    userInfo.setDescription("祂以语言间浮现的罪孽为食");
    userInfo.setPhone("777777777");
    userInfo.setAvatar(avatar);

    return userInfo;
}

// 生成默认的 MessageInfo 对象. 文本消息
bite_im::MessageInfo makeTextMessageInfo(int index, const QString& chatSessionId, const QByteArray& avatar) {
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    bite_im::StringMessageInfo stringMessageInfo;
    stringMessageInfo.setContent("这是一条消息内容" + QString::number(index));

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::STRING);
    messageContent.setStringMessage(stringMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

bite_im::MessageInfo makeImageMessageInfo(int index, const QString& chatSessionId, const QByteArray& avatar) {
    bite_im::MessageInfo messageInfo;
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setSender(makeUserInfo(index, avatar));

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::IMAGE);

    // 真实服务器推送的消息数据里, 本身也就不带图片的正文. 只是 fileId.
    // 需要通过 fileId, 二次发起请求, 通过 getSingleFile 接口来获取到内容.
    // imageMessageInfo.setImageContent();

    bite_im::ImageMessageInfo imageMessageInfo;
    imageMessageInfo.setFileId("testImage");


    messageContent.setImageMessage(imageMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

bite_im::MessageInfo makeFileMessageInfo(int index, const QString& chatSessionId, const QByteArray& avatar) {
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    bite_im::FileMessageInfo fileMessageInfo;
    fileMessageInfo.setFileId("testFile");
    // 真实服务器推送的消息数据里, 本身也就不带图片的正文. 只是 fileId.
    // 需要通过 fileId, 二次发起请求, 通过 getSingleFile 接口来获取到内容.
    fileMessageInfo.setFileName("test.txt");
    // 此处文件大小, 无法设置. 由于 fileSize 属性, 不是 optional , 此处先设置一个 0 进来
    fileMessageInfo.setFileSize(0);

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::FILE);
    messageContent.setFileMessage(fileMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

bite_im::MessageInfo makeSpeechMessageInfo(int index, const QString& chatSessionId, const QByteArray& avatar) {
    bite_im::MessageInfo messageInfo;
    messageInfo.setMessageId(QString::number(3000 + index));
    messageInfo.setChatSessionId(chatSessionId);
    messageInfo.setTimestamp(getTime());
    messageInfo.setSender(makeUserInfo(index, avatar));

    bite_im::SpeechMessageInfo speechMessageInfo;
    // 真实服务器推送的消息数据里, 本身也就不带图片的正文. 只是 fileId.
    // 需要通过 fileId, 二次发起请求, 通过 getSingleFile 接口来获取到内容.
    speechMessageInfo.setFileId("testSpeech");

    bite_im::MessageContent messageContent;
    messageContent.setMessageType(bite_im::MessageTypeGadget::MessageType::SPEECH);
    messageContent.setSpeechMessage(speechMessageInfo);
    messageInfo.setMessage(messageContent);
    return messageInfo;
}

/// //////////////////////////////////////////////////////////////////////
///
HttpServer* HttpServer::instance = nullptr;

HttpServer *HttpServer::getInstance()
{
    if(instance == nullptr)
    {
        instance = new HttpServer();
    }

    return instance;
}

bool HttpServer::initHttpServer()
{
    //绑定套接字
    int ret = tcpServer.listen(QHostAddress::Any,8000);
    httpServer.bind(&tcpServer);

    ////////////////////////////////////////////////////////////////////
    /// 配置路由
    ////////////////////////////////////////////////////////////////////
    /// 配置ping
    httpServer.route("/ping",[=](const QHttpServerRequest& req){
        (void)req;
        qDebug() << "[http] 收到ping请求";
        return "pong";
    });

    /// 获取个人用户信息
    httpServer.route("/service/user/get_user_info",[=](const QHttpServerRequest& req){
        return getUserInfo(req);
    });

    /// 获取好友列表
    httpServer.route("/service/friend/get_friend_list",[=](const QHttpServerRequest& req){
        return getFriendList(req);
    });

    /// 获取消息列表
    httpServer.route("/service/friend/get_chat_session_list",[=](const QHttpServerRequest& req){
        return getChatSessionList(req);
    });

    /// 获取好友申请列表
    httpServer.route("/service/friend/get_pending_friend_events",[=](const QHttpServerRequest& req){
        return getApplyList(req);
    });

    /// 获取最近消息列表
    httpServer.route("/service/message_storage/get_recent",[=](const QHttpServerRequest& req){
        return getRecent(req);
    });
    /// 发送消息
    httpServer.route("/service/message_transmit/new_message",[=](const QHttpServerRequest& req){
        return newMessage(req);
    });
    /// 修改昵称
    httpServer.route("/service/user/set_nickname",[=](const QHttpServerRequest& req){
        return setNickname(req);
    });
    /// 修改签名
    httpServer.route("/service/user/set_description",[=](const QHttpServerRequest& req){
        return setDesc(req);
    });
    /// 获取验证码
    httpServer.route("/service/user/get_phone_verify_code",[=](const QHttpServerRequest& req){
        return getVerifyCode(req);
    });

    /// 修改手机号
    httpServer.route("/service/user/set_phone",[=](const QHttpServerRequest& req){
        return setPhone(req);
    });

    /// 修改头像
    httpServer.route("/service/user/set_avatar",[=](const QHttpServerRequest& req){
        return setAvatar(req);
    });

    /// 删除好友
    httpServer.route("/service/friend/remove_friend",[=](const QHttpServerRequest& req){
        return removeFriend(req);
    });

    /// 申请好友
    httpServer.route("/service/friend/add_friend_apply",[=](const QHttpServerRequest& req){
        return addFriendApply(req);
    });

    /// 好友申请处理(同意 or 拒绝情况)
    httpServer.route("/service/friend/add_friend_process",[=](const QHttpServerRequest& req){
        return addFriendProcess(req);
    });

    /// 创建群聊会话
    httpServer.route("/service/friend/create_chat_session",[=](const QHttpServerRequest& req){
        return createChatSession(req);
    });

    /// 获取群聊会话成员列表
    httpServer.route("/service/friend/get_chat_session_member",[=](const QHttpServerRequest& req){
        return getChatSessionMember(req);
    });

    /// 搜索用户
    httpServer.route("/service/friend/search_friend",[=](const QHttpServerRequest& req){
        return searchFriend(req);
    });

    /// 通过关键词搜索历史消息
    httpServer.route("/service/message_storage/search_history",[=](const QHttpServerRequest& req){
        return searchHistory(req);
    });

    /// 通过时间获取历史消息
    httpServer.route("/service/message_storage/get_history",[=](const QHttpServerRequest& req){
        return getHistory(req);
    });

    /// 通过用户密码登录
    httpServer.route("/service/user/username_login",[=](const QHttpServerRequest& req){
        return usernameLogin(req);
    });

    /// 通过用户密码注册
    httpServer.route("/service/user/username_register",[=](const QHttpServerRequest& req){
        return usernameRegister(req);
    });

    /// 通过电话登录
    httpServer.route("/service/user/phone_login",[=](const QHttpServerRequest& req){
        return phoneLogin(req);
    });

    /// 通过电话注册
    httpServer.route("/service/user/phone_register",[=](const QHttpServerRequest& req){
        return phoneRegister(req);
    });

    httpServer.route("/service/speech/recognition", [=](const QHttpServerRequest& req) {
        return this->recognition(req);
    });

    if(ret == 8000)
    {
        QCoreApplication::exec();
        return true;
    }
    else
        return false;
}
////////////////////////////////////////////////////////////////////////////////////////////
/// 路由相关函数
////////////////////////////////////////////////////////////////////////////////////////////
QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest &req)
{
    //解析 bite 请求
    bite_im::GetUserInfoReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 获取用户信息] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建 bite 响应
    bite_im::GetUserInfoRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setUserInfo(makeUserInfo(111,loadFileToByteArray(":/image/groupAvatar.png")));

    QByteArray body = pbResp.serialize(&pbSerializer);

    //Qt 6.8 创建报头, 非 6.8 无法使用setHeader
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //构造Http 响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest &req)
{
    //解析 bite 请求
    bite_im::GetFriendListReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 获取好友列表] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建 bite 响应
    bite_im::GetFriendListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToByteArray(":/image/cross.png");

    QList<bite_im::UserInfo> friendList;

    for (int i = 0; i < 20; ++i) {
        bite_im::UserInfo userInfo = makeUserInfo(i, avatar);
        friendList.push_back(userInfo);
    }
    pbResp.setFriendList(friendList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //构造Http 响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //构造Http 响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionList(const QHttpServerRequest &req)
{
    //解析bite请求
    bite_im::GetChatSessionListReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 获取消息列表] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建bite响应
    bite_im::GetChatSessionListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QList<bite_im::ChatSessionInfo> chatSessionInfoList;

    QByteArray avatar = loadFileToByteArray(":/resource/image/defaultAvatar.png");

    //单聊会话逻辑
    for(int i = 0; i <= 20;i++)
    {
        bite_im::ChatSessionInfo chatSessionInfo;

        chatSessionInfo.setSingleChatFriendId(QString::number(111 + i));
        chatSessionInfo.setChatSessionId(QString::number(1111 + i));
        chatSessionInfo.setChatSessionName("测试会话" + QString::number(1111 + i));
        chatSessionInfo.setPrevMessage(makeTextMessageInfo(i,QString::number(1111 + i),avatar));
        chatSessionInfo.setAvatar(avatar);

        chatSessionInfoList.push_back(chatSessionInfo);

    }
    //群聊会话逻辑
    bite_im::ChatSessionInfo groupChatSessionInfo;
    groupChatSessionInfo.setSingleChatFriendId("");
    groupChatSessionInfo.setChatSessionId(QString::number(2000));
    groupChatSessionInfo.setChatSessionName("测试群聊会话");
    groupChatSessionInfo.setPrevMessage(makeTextMessageInfo(0,QString::number(2000),avatar));
    groupChatSessionInfo.setAvatar(avatar);

    chatSessionInfoList.push_back(groupChatSessionInfo);

    pbResp.setChatSessionInfoList(chatSessionInfoList);

    //进行序列化

    QByteArray body = pbResp.serialize(&pbSerializer);

    //构造Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //构造完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::GetPendingFriendEventListReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());

    LOG() << "[REQ 获取好友申请列表] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::GetPendingFriendEventListRsp pbResp;

    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QList<bite_im::FriendEvent> eventList;

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    for(int i = 0; i <= 15;i++)
    {
        bite_im::FriendEvent event;
        event.setEventId("");
        event.setSender(makeUserInfo(i + 765,avatar));
        eventList.push_back(event);
    }

    pbResp.setEvent(eventList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getRecent(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::GetRecentMsgReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());

    LOG() << "[REQ 获取最近消息] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::GetRecentMsgRsp pbResp;

    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QList<bite_im::MessageInfo> recentMessageList;

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    ////////////////////////////////////
    /// 测试用例
    for(int i = 0; i <= 30;i++)
    {
        recentMessageList.push_back(makeTextMessageInfo(i,"2000",avatar));
    }

    recentMessageList.push_back(makeImageMessageInfo(31,"2000",avatar));

    recentMessageList.push_back(makeFileMessageInfo(32,"2000",avatar));

    //////////////////////////////////////

    pbResp.setMsgList(recentMessageList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::newMessage(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::NewMessageReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 发送信息] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::NewMessageRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::setNickname(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::SetUserNicknameReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 修改昵称] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::SetUserNicknameRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;

}

QHttpServerResponse HttpServer::setDesc(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::SetUserDescriptionReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 修改签名] requestId = " << pbReq.requestId() << " loginSessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::SetUserDescriptionRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;

}

QHttpServerResponse HttpServer::getVerifyCode(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::PhoneVerifyCodeReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 获取验证码] requestId = " << pbReq.requestId();

    //创建响应
    bite_im::PhoneVerifyCodeRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setVerifyCodeId("testVerifyCodeId");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::SetUserPhoneNumberReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 修改手机号] requestId = " << pbReq.requestId()
          << "sessionId = " << pbReq.sessionId()
          << "phone_number = " << pbReq.phoneNumber()
          << "phone_verify_code_id = " << pbReq.phoneVerifyCodeId()
          << "phone_verify_code =" << pbReq.phoneVerifyCode();



    //创建响应
    bite_im::SetUserPhoneNumberRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::SetUserAvatarReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 修改头像] requestId = " << pbReq.requestId()
          << "sessionId = " << pbReq.sessionId();

    //创建响应
    bite_im::SetUserAvatarRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    bite_im::FriendRemoveReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 删除好友] requestId = " << pbReq.requestId()
          << "sessionId = " << pbReq.sessionId()
          << "peer_id = " << pbReq.peerId();

    bite_im::FriendRemoveRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    bite_im::FriendAddReq pbReq;
    pbReq.deserialize(&pbSerializer,req.body());
    LOG() << "[REQ 删除好友] requestId = " << pbReq.requestId()
          << "sessionId = " << pbReq.sessionId()
          << "user_id = " << pbReq.userId()
          << "respondent_id = " << pbReq.respondentId();

    bite_im::FriendAddRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setNotifyEventId("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::addFriendProcess(const QHttpServerRequest &req)
{
    // 解析请求
    bite_im::FriendAddProcessReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 处理好友申请] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId()
          << ", applyUserId=" << pbReq.applyUserId();

    // 构造响应
    bite_im::FriendAddProcessRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::createChatSession(const QHttpServerRequest &req)
{
    // 解析请求
    bite_im::ChatSessionCreateReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 创建群聊会话] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    bite_im::ChatSessionCreateRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionMember(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::GetChatSessionMemberReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 获取群聊会话成员列表] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    bite_im::GetChatSessionMemberRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    QList<bite_im::UserInfo> memberInfoList;

    for (int i = 0; i < 10; ++i) {
        bite_im::UserInfo userInfo = makeUserInfo(1000 + i, avatar);
        memberInfoList.push_back(userInfo);
    }

    pbResp.setMemberInfoList(memberInfoList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;

}

QHttpServerResponse HttpServer::searchFriend(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::FriendSearchReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 搜索好友] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    bite_im::FriendSearchRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    QList<bite_im::UserInfo> searchFriendList;

    for (int i = 0; i < 10; ++i) {
        bite_im::UserInfo userInfo = makeUserInfo(1000 + i, avatar);
        searchFriendList.push_back(userInfo);
    }

    pbResp.setUserInfo(searchFriendList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::searchHistory(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::MsgSearchReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 关键词搜索历史消息] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    bite_im::MsgSearchRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QList<bite_im::MessageInfo> msgList;

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    for (int i = 0; i < 10; ++i) {
        bite_im::MessageInfo msgInfo = makeTextMessageInfo(i,"2000",avatar);
        msgList.push_back(msgInfo);
    }

    pbResp.setMsgList(msgList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getHistory(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::GetHistoryMsgReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 时间搜索历史消息] request_id=" << pbReq.requestId()
          << ",loginSessionId=" << pbReq.sessionId();

    // 构造响应
    bite_im::MsgSearchRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QList<bite_im::MessageInfo> msgList;

    QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

    for (int i = 0; i < 10; ++i) {
        bite_im::MessageInfo msgInfo = makeTextMessageInfo(i,"2000",avatar);
        msgList.push_back(msgInfo);
    }

    pbResp.setMsgList(msgList);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::usernameLogin(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::UserLoginReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 用户密码登录] request_id=" << pbReq.requestId();

    // 构造响应
    bite_im::UserLoginRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setLoginSessionId("2000");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::usernameRegister(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::UserRegisterReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 用户密码注册] request_id=" << pbReq.requestId();

    // 构造响应
    bite_im::UserRegisterRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::phoneLogin(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::PhoneLoginReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 电话登录] request_id=" << pbReq.requestId();

    // 构造响应
    bite_im::PhoneLoginRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setLoginSessionId("2000");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::phoneRegister(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::PhoneRegisterReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 电话登录] request_id=" << pbReq.requestId();

    // 构造响应
    bite_im::PhoneRegisterRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;
}

QHttpServerResponse HttpServer::getSingleFile(const QHttpServerRequest &req)
{
    //解析请求
    bite_im::GetSingleFileReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 获取文件内容] request_id=" << pbReq.requestId();

    //构建响应
    bite_im::GetSingleFileRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    bite_im::FileDownloadData fileDownloadData;
    fileDownloadData.setFileId(pbReq.fileId());
    if (pbReq.fileId() == "imageFileId") {
        fileDownloadData.setFileContent(loadFileToByteArray(":/image/cat.jpg"));
    } else if (pbReq.fileId() == "fileFileId") {
        fileDownloadData.setFileContent(loadFileToByteArray(":/file/test.txt"));
    } else if (pbReq.fileId() == "soundFileId") {
        fileDownloadData.setFileContent(loadFileToByteArray(":/file/sound.pcm"));
    }

    pbResp.setFileData(fileDownloadData);

    QByteArray body = pbResp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);

    return httpResp;

}

QHttpServerResponse HttpServer::recognition(const QHttpServerRequest &req)
{
    bite_im::SpeechRecognitionReq pbReq;
    pbReq.deserialize(&pbSerializer, req.body());
    LOG() << "[REQ 语⾳转⽂字] request_id=" << pbReq.requestId();
    // 构造响应
    bite_im::SpeechRecognitionRsp pbRsp;
    pbRsp.setRequestId(pbReq.requestId());
    pbRsp.setSuccess(true);
    pbRsp.setErrmsg("");
    pbRsp.setRecognitionResult("这是⼀段录⾳这是⼀段录⾳");
    QByteArray body = pbRsp.serialize(&pbSerializer);

    //创建Http响应报头
    QHttpHeaders httpHeader;
    httpHeader.append("Content-Type","application/x-protobuf");

    //创建完整Http响应
    QHttpServerResponse httpResp(body, QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeaders(httpHeader);
    return httpResp;
}
///////////////////////////////////////////////////
///
////////////////////////////////////////////////////
WebSocketServer* WebSocketServer::instance = nullptr;

WebSocketServer *WebSocketServer::getInstance()
{
    if(instance == nullptr)
    {
        instance = new WebSocketServer();
    }

    return instance;
}

bool WebSocketServer::initWebSocketServer()
{
    int ret = webSocketServer.listen(QHostAddress::Any,8001);
    connect(&webSocketServer,&QWebSocketServer::newConnection,this,[=](){
        QWebSocket* socket = webSocketServer.nextPendingConnection();
            qDebug() << "[webSocket] newConnection";

        //断开连接
        connect(socket,&QWebSocket::disconnected,this,[=](){
            qDebug() << "[webSocket] disconnected";
            socket->deleteLater();
            //////////////////////////////////////////////////// 断开所有测试连接
            disconnect(this,&WebSocketServer::sendTextResp,this, nullptr);
            disconnect(this, &WebSocketServer::sendImageResp, this, nullptr);
            disconnect(this, &WebSocketServer::sendFileResp, this, nullptr);
            disconnect(this, &WebSocketServer::sendSpeechResp, this, nullptr);


            disconnect(this,&WebSocketServer::sendFriendRemove,this, nullptr);
            disconnect(this,&WebSocketServer::sendAddFriendApply,this, nullptr);
            disconnect(this,&WebSocketServer::sendAddFriendProcess,this,nullptr);
            disconnect(this, &WebSocketServer::sendCreateChatSession, this, nullptr);


        });

        //连接出错
        connect(socket,&QWebSocket::errorOccurred,this,[=](QAbstractSocket::SocketError error){
            qDebug() << "[webSocket] errorOccurred: " << error;
        });

        //发送文本消息
        connect(socket,&QWebSocket::textMessageReceived,this,[=](const QString& message){
            qDebug() << "[webSocket] textMessageReceived" << message;
            if(message == "ping")
            {
                socket->sendTextMessage("pong");
                return;
            }
        });

        //发送二进制消息
        connect(socket,&QWebSocket::binaryMessageReceived,this,[=](){
        qDebug() << "[webSocket] binaryMessageReceived";
        });

        //////////////////////////////////////////////////////////////////////////////////////////
        /// 测试功能
        //////////////////////////////////////////////////////////////////////////////////////////
        /// 测试 - 文本消息
        connect(this,&WebSocketServer::sendTextResp,this, [=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 发送文本消息失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);

            bite_im::MessageInfo messageInfo = makeTextMessageInfo(this->messageIndex++,"2000",loadFileToByteArray(":/image/defaultAvatar.png"));

            bite_im::NotifyNewMessage newMessage;
            newMessage.setMessageInfo(messageInfo);

            message.setNewMessageInfo(newMessage);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);

            LOG() << "WebSocketServer 发送文本消息成功: " << messageInfo.message().stringMessage().content();
        });

        /// 测试 - 图片消息
        connect(this,&WebSocketServer::sendImageResp,this, [=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 发送图片消息失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);

            bite_im::MessageInfo messageInfo = makeImageMessageInfo(this->messageIndex++,"2000",loadFileToByteArray(":/image/defaultAvatar.png"));

            bite_im::NotifyNewMessage newMessage;
            newMessage.setMessageInfo(messageInfo);

            message.setNewMessageInfo(newMessage);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);

            LOG() << "WebSocketServer 发送图片消息成功: ";
        });

        /// 测试 - 文件消息
        connect(this,&WebSocketServer::sendFileResp,this, [=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 发送文件消息失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);

            bite_im::MessageInfo messageInfo = makeFileMessageInfo(this->messageIndex++,"2000",loadFileToByteArray(":/image/defaultAvatar.png"));

            bite_im::NotifyNewMessage newMessage;
            newMessage.setMessageInfo(messageInfo);

            message.setNewMessageInfo(newMessage);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);

            LOG() << "WebSocketServer 发送文件消息成功: ";
        });

        /// 测试 - 语音消息
        connect(this,&WebSocketServer::sendSpeechResp,this, [=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 发送语音消息失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_MESSAGE_NOTIFY);

            bite_im::MessageInfo messageInfo = makeSpeechMessageInfo(this->messageIndex++,"2000",loadFileToByteArray(":/file/speech.pcm"));

            bite_im::NotifyNewMessage newMessage;
            newMessage.setMessageInfo(messageInfo);

            message.setNewMessageInfo(newMessage);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);

            LOG() << "WebSocketServer 发送语音消息成功: ";
        });


        /// 测试 - 删除好友
        connect(this,&WebSocketServer::sendFriendRemove,this,[=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 删除好友失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_REMOVE_NOTIFY);

            bite_im::NotifyFriendRemove friendRM;
            friendRM.setUserId(QString::number(deleteOfUserId));

            message.setFriendRemove(friendRM);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);

            LOG() << "WebSocketServer 发送删除好友提示成功, 你被该用户删除: " << friendRM.userId();
        });

        /// 测试 - 发送好友申请
        connect(this, &WebSocketServer::sendAddFriendApply,this,[=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 申请好友失败! 原因: socket对象无效!";
                return;
            }

            //构造响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_APPLY_NOTIFY);

            bite_im::NotifyFriendAddApply friendAddApply;

            QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");

            friendAddApply.setUserInfo(makeUserInfo(0,avatar));
            message.setFriendAddApply(friendAddApply);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);
            LOG() << "WebSocketServer 发送申请好友成功, 向该用户申请: " << friendAddApply.userInfo().userId();
        });

        /// 测试 - 好友申请同意/拒绝逻辑
        connect(this, &WebSocketServer::sendAddFriendProcess,this,[=](bool agree){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 申请好友失败! 原因: socket对象无效!";
                return;
            }

            //构建响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::FRIEND_ADD_PROCESS_NOTIFY);

            bite_im::NotifyFriendAddProcess process;
            process.setAgree(agree);

            QByteArray imageData = loadFileToByteArray(":/image/defaultAvatar.png");

            process.setUserInfo(makeUserInfo(0,imageData));
            message.setFriendProcessResult(process);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);
            LOG() << "WebSocketServer 通知用户好友申请结果成功, 用户id: " << process.userInfo().userId() << ",申请结果: "<< agree;

        });

        /// 测试 - 创建群聊会话逻辑
        connect(this, &WebSocketServer::sendCreateChatSession,this,[=](){
            //判断socket是否有效
            if(socket == nullptr || !socket->isValid())
            {
                LOG() << "WebSocketServer 申请好友失败! 原因: socket对象无效!";
                return;
            }

            //构建响应数据
            bite_im::NotifyMessage message;
            message.setNotifyEventId("");
            message.setNotifyType(bite_im::NotifyTypeGadget::NotifyType::CHAT_SESSION_CREATE_NOTIFY);

            bite_im::NotifyNewChatSession notifyChatSession;

            bite_im::ChatSessionInfo chatSessionInfo;

            chatSessionInfo.setChatSessionId("2100");
            chatSessionInfo.setSingleChatFriendId("");
            chatSessionInfo.setChatSessionName("新的群聊");
            QByteArray avatar = loadFileToByteArray(":/image/defaultAvatar.png");
            chatSessionInfo.setAvatar(avatar);

            notifyChatSession.setChatSessionInfo(chatSessionInfo);
            message.setNewChatSessionInfo(notifyChatSession);

            QByteArray body = message.serialize(&pbSerializer);

            socket->sendBinaryMessage(body);
            LOG() << "WebSocketServer 通知用户创建群聊会话成功";

        });
    });

    return ret == 8001;
}






































